# -*- coding: utf-8 -*-
"""Oman_Aranet_Analyzer.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/17wASkn_uTf-_auavzP2b4ea4tS5SW40R

This code includes the sentiment and emotional analysis conducted on the collected data for Oman. We repeated the same procedure for each of the GCC countries. To replicate the analysis, make sure to include the right data path and follow the same format of the dataframes below.
"""

#Install Aranet
pip install git+https://github.com/UBC-NLP/AraNet

from aranet import aranet
#from keras_preprocessing.sequence import pad_sequences

#Import Data Server
from google.colab import drive
drive.mount('/content/drive')

#load AraNet sentiment model
model_path = '/content/drive/MyDrive/Twitter GCC/AraNet Models/Models/sentiment_aranet'
senti_obj = aranet.AraNet(model_path)

#load AraNet emotion model
model_path = '/content/drive/MyDrive/Twitter GCC/AraNet Models/Models/emotion_aranet'
emo_obj = aranet.AraNet(model_path)

#load AraNet dialect model
model_path = "/content/drive/MyDrive/Twitter GCC/AraNet Models/Models/dialect_aranet/"
dialect_obj = aranet.AraNet(model_path)

#Test Models
text_str ="الله عليكي و انتي دائما مفرحانا"
print(senti_obj.predict(text=text_str))
print(emo_obj.predict(text=text_str))
print(dialect_obj.predict(text=text_str))

#Basic Libraries
import numpy as np
import pandas as pd

#Load data
dataloc='/content/drive/MyDrive/Twitter GCC/Final Data/Oman_Dublicate_Removed_Tweets.pkl'
data = pd.read_pickle(dataloc)

def AranetSentiment(text_str):
    return senti_obj.predict(text=text_str)

def AranetEmotion(text_str):
    return emo_obj.predict(text=text_str)

def AranetDialect(text_str):
    return dialect_obj.predict(text=text_str)

data['AranetSentimentA'] = data['Text'].apply(lambda x: AranetSentiment(x)[0][0])
#It took to excute this line 1 hour and 5 min.

data.to_pickle('/content/drive/MyDrive/Twitter GCC/Final Data/New-Unchecked-Data/Oman_Analyzed_Partial')

data.to_csv('/content/drive/MyDrive/Twitter GCC/Final Data/New-Unchecked-Data/Oman_Analyzed_Partial.csv', sep=',',encoding = 'utf-8', index=False)

#Get the percentage of positive tweets
ptweets = data[data.AranetSentimentA == 'pos']
ptweets = ptweets['Text']
#Get the percentage of Neutral tweets
neutweets = data[data.AranetSentimentA == 'nu']
neutweets = neutweets['Text']
#Get the percentage of Negative tweets
negtweets = data[data.AranetSentimentA == 'neg']
negtweets = negtweets['Text']



print('Percentage of positive tweets = ' , round(((ptweets.shape[0]/data.shape[0])*100),1))
print()
print('Percentage of Neutral tweets = ', round(((neutweets.shape[0]/data.shape[0])*100),1))
print()
print('Percentage of Negative tweets = ',round(((negtweets.shape[0]/data.shape[0])*100),1))

data['AranetEmotionA'] = data['Text'].apply(lambda x: AranetEmotion(x)[0][0])
# 1 hour 25 min

data.to_pickle('/content/drive/MyDrive/Twitter GCC/Final Data/New-Unchecked-Data/Oman_Analyzed')

data.to_csv('/content/drive/MyDrive/Twitter GCC/Final Data/New-Unchecked-Data/Oman_Analyzed.csv', sep=',',encoding = 'utf-8', index=False)

data